﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Register.BLL;
using System.Web.Script.Serialization;

namespace RegisterWeb.Manager.RegistrationFeeManager.ashx
{
    /// <summary>
    /// RegisterService 的摘要说明
    /// </summary>
    public class RegisterService : IHttpHandler
    {
        static List<RegisterJSON> listjson = new List<RegisterJSON>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];      
            if (action == "get")
            {
                listjson.Clear();
                //   List<PayForRegistrantionJSON> listjson = new List<PayForRegistrantionJSON>();
                int flag = 1;
                StringBuilder strBuilder = new StringBuilder();
                int intPageSize = int.Parse(context.Request["rows"].ToString());
                int intCurrentPage = int.Parse(context.Request["page"].ToString());
                //  var list = PayForRegistrantionInfoBLL.GetAllPayForRegistrantion("1=1", intCurrentPage, intPageSize);
                string Departments_ID = (string)context.Request["departments"];
                string OutpatientType = (string)context.Request["rfoutpatientype"];
                string dt1 = (string)context.Request["date1"];
                string dt2 = (string)context.Request["date2"];

                if (!String.IsNullOrEmpty(Departments_ID))
                {
                    strBuilder.Append(" Departments_ID='").Append(Departments_ID).Append("' ");
                    flag = 0;
                }
                if (!String.IsNullOrEmpty(OutpatientType))
                {
                    if (flag == 1)
                    {
                        strBuilder.Append(" Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                    }
                    else
                        strBuilder.Append(" and  Outpatient_OutpaitentType='").Append(OutpatientType).Append("' ");
                    flag = 0;
                }
                if (!String.IsNullOrEmpty(dt1))
                {

                    DateTime date1 = Convert.ToDateTime(dt1);
                    if (flag == 1)
                    {
                        strBuilder.Append(" PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                    }
                    else
                        strBuilder.Append(" and PayForRegistrantion_AddTime>'").Append(date1).Append("' ");
                    flag = 0;
                }

                if (!String.IsNullOrEmpty(dt2))
                {

                    DateTime date2 = Convert.ToDateTime(dt2);
                    if (flag == 1)
                    {
                        strBuilder.Append(" PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                    }
                    else
                        strBuilder.Append(" and PayForRegistrantion_AddTime<'").Append(date2).Append("' ");
                    flag = 0;
                }

                if (flag == 1)
                {
                    strBuilder.Append("1=1");
                }
                var list = RegisterviewBLL.GetList(strBuilder.ToString(), intCurrentPage, intPageSize);

                foreach (var row in list)
                {
                    RegisterJSON json1 = new RegisterJSON(row.Register_ID, row.Outpatient_ID, row.Appointment_ID, row.Outpatient_OutpatientType, row.Departments_ID, row.Hospital_ID, row.Departments_Name, row.Hospital_Name, row.PayForRegistrantion_ID,row.Register_AddTime ,row.Register_PatientName);
                    listjson.Add(json1);
                }
                RegisterJSONjsons json = new RegisterJSONjsons();
                json.rows = listjson;             
                json.total = RegisterviewBLL.GetListCount(strBuilder.ToString());
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string ss = jss.Serialize(json);
                context.Response.Write(ss);
                action = action;
            }
            if (action == "getExcel")
            {

                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpResponse resp;
                resp = context.Response;// Page.Response;
                resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                resp.AppendHeader("Content-Disposition", "attachment;filename=" + "Excel.xls");
                string colHeaders = "", ls_item = "";
                colHeaders += "编号" + "\t";
                colHeaders += "医院名" + "\t";
                colHeaders += "科室名" + "\t";
                colHeaders += "门诊类型" + "\t";
                colHeaders += "病人姓名" + "\t";
                colHeaders += "添加时间" + "\n";
              
                resp.Write(colHeaders);
                //向HTTP输出流中写入取 得的数据信息 
                //逐行处理数据   
                for (int i = 0; i < listjson.Count; i++)
                {
                    ls_item = ls_item + listjson[i].Register_ID + "\t";
                    ls_item = ls_item + listjson[i].Hospital_Name + "\t";
                    ls_item = ls_item + listjson[i].Departments_Name + "\t";
                    ls_item = ls_item + listjson[i].Outpatient_OutpaitentTypeName + "\t";
                    ls_item = ls_item + listjson[i].Register_PatientName + "\t";
                    ls_item = ls_item + listjson[i].Register_AddTime + "\n";
                    resp.Write(ls_item);
                    ls_item = "";
                }
                resp.End();
                //  json.total = PayForRegistrantionInfoBLL.GetPayForRegistrantionCount("1=1");
                // JavaScriptSerializer jss = new JavaScriptSerializer()
                //string ss = jss.Serialize(json);     
                //context.Response.Write(ss);

            }
        }
        class RegisterJSONjsons
        {
            public int total { get; set; }
            public List<RegisterJSON> rows { get; set; }

        }
        class       RegisterJSON
        {
            public String Register_ID
            { get; set; }
            public String Outpatient_ID
            { get; set; }
            public String Appointment_ID
            { get; set; }
            public String Outpatient_OutpatientType
            { get; set; }
            public String Departments_ID
            { get; set; }
            public String Hospital_ID
            { get; set; }
            public String Departments_Name
            { get; set; }
            public String Hospital_Name
            { get; set; }
            public String PayForRegistrantion_ID
            { get; set; }
            public String Register_AddTime
            { get; set; }
            public String Outpatient_OutpaitentTypeName
            { get; set; }
            public String Register_PatientName
            { get; set; }
            public RegisterJSON(String Register_ID, String Outpatient_ID, String Appointment_ID, String Outpatient_OutpatientType, String Departments_ID, String Hospital_ID, String Departments_Name, String Hospital_Name, String PayForRegistrantion_ID, DateTime Register_AddTime, String Register_PatientName)
            {
                this.Register_ID = Register_ID;
                this.Outpatient_ID = Outpatient_ID;
                this.Appointment_ID = Appointment_ID;
                this.Outpatient_OutpatientType = Outpatient_OutpatientType;
                this.Departments_ID = Departments_ID;
                this.Hospital_ID = Hospital_ID;
                this.Departments_Name = Departments_Name;
                this.Hospital_Name = Hospital_Name;
                this.Outpatient_OutpaitentTypeName = ParamterHelper.GetParameterNameByTypeAndID("regType", Outpatient_OutpatientType);
                this.Register_AddTime = Register_AddTime.ToString("yyyy-MM-dd");
                this.Register_PatientName = Register_PatientName;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}